package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.groundpush.X3SelectGiftBalanceAdapter;
import com.das.mechanic_base.adapter.groundpush.X3SelectGiftRecordAdapter;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.bean.main.GiftBalanceBean;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * 赠送余额
 */
public class X3BottomGiftBalanceDialog extends X3BaseBottomSheetDialog implements View.OnClickListener {

    private TextView tv_cancel;
    private Button gift_btn_sure;
    private EditText gift_money_edt;
    private ImageView iv_icon_img;//赠送对象头像
    private TextView carowner_name_tv;//名称
    private TextView carowner_tell_tv;//手机号
    //自定义钱数，第一个adapter
    private RecyclerView gift_money_recycler_view;
    private X3SelectGiftBalanceAdapter selectGiftBalanceAdapter;
    private List<String> GiftBalanceList;
    private boolean hasFocu;
    //赠送记录，第二个adapter
    private RecyclerView gift_record_recycler_view;
    private X3SelectGiftRecordAdapter selectGiftRecordAdapter;
    private X3BottomGiftBalanceTWDialog bottomGiftBalanceTWDialog;//跳转到确定下一步

    private int presentLimitDailyRemainder;//赠送额度
    private int selectMoney;//选中的金钱
    private GiftBalanceBean giftBalanceBean;
    private int carOwnerNameUserId;//赠送余额的时候，将这个id一直传下去，提交的时候需要用到
    private String customMoney;//自定义余额提示
    private String moneyUnit;

    public X3BottomGiftBalanceDialog(@NonNull Context context) {
        super(context);
    }

    public void setData(GiftBalanceBean giftBalanceBeans,int carOwnerNameUserId) {
        if(giftBalanceBeans.getPresentLimitDailyRemainder()>0){
            customMoney=mContext.getString(R.string.x3_gift_money_custom_left_tv) + String.format(mContext.getString(R.string.x3_gift_money_custom_right_tv),giftBalanceBeans.getPresentLimitDailyRemainder() + ")",moneyUnit);
        }else {
            customMoney=mContext.getString(R.string.x3_gift_money_custom_left_tv)+ String.format(mContext.getString(R.string.x3_gift_money_custom_right_tv),"0)",moneyUnit);
        }
        this.giftBalanceBean = giftBalanceBeans;
        this.carOwnerNameUserId=carOwnerNameUserId;
        setMoneyEdtClickStyle();
        presentLimitDailyRemainder = (int) giftBalanceBeans.getPresentLimitDailyRemainder();
        //设置头像图片
        X3GlideNewUtils.loadRoundCornerImage(mContext, X3StringUtils.getImageUrl(giftBalanceBeans.getImgUrl()), iv_icon_img,4,R.mipmap.x3_car_img_bg);
        carowner_name_tv.setText(TextUtils.isEmpty(giftBalanceBeans.getCarOwnerName()) ? "" : giftBalanceBeans.getCarOwnerName());
        carowner_tell_tv.setText(TextUtils.isEmpty(giftBalanceBeans.getMobile()) ? "" : giftBalanceBeans.getMobile());
        selectGiftRecordAdapter.changeData(giftBalanceBeans.getCreditPresentVoList());
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        moneyUnit = (String) SpHelper.getData(X3Constent.MONEYCODE, "£");
        tv_cancel = getView(R.id.tv_cancel);
        gift_btn_sure = getView(R.id.gift_btn_sure);
        gift_money_recycler_view = getView(R.id.gift_money_recycler_view);
        gift_money_edt = getView(R.id.gift_money_edt);
        gift_record_recycler_view = getView(R.id.gift_record_recycler_view);
        iv_icon_img = getView(R.id.iv_icon_img);
        carowner_name_tv = getView(R.id.carowner_name_tv);
        carowner_tell_tv = getView(R.id.carowner_tell_tv);

        tv_cancel.setOnClickListener(this);
        gift_btn_sure.setOnClickListener(this);

        //金额
        gift_money_recycler_view.setLayoutManager(new GridLayoutManager(mContext, 3));
        selectGiftBalanceAdapter = new X3SelectGiftBalanceAdapter(mContext);
        gift_money_recycler_view.setAdapter(selectGiftBalanceAdapter);
        //设置数据
        GiftBalanceList = new ArrayList<>();
        GiftBalanceList.add("50");
        GiftBalanceList.add("100");
        GiftBalanceList.add("200");
        GiftBalanceList.add("300");
        GiftBalanceList.add("500");
        GiftBalanceList.add("1000");
        selectGiftBalanceAdapter.changeSelect(GiftBalanceList);

        //本店历史赠送记录
        gift_record_recycler_view.setLayoutManager(new LinearLayoutManager(mContext));
        selectGiftRecordAdapter = new X3SelectGiftRecordAdapter(mContext);
        gift_record_recycler_view.setAdapter(selectGiftRecordAdapter);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_gift_balance_bottom_dialog;
    }

    private void setMoneyEdtClickStyle() {
        gift_money_edt.setHint(customMoney);
        gift_money_edt.setOnFocusChangeListener((v, hasFocus) -> {
            hasFocu = hasFocus;
            if (hasFocus) {
                // 获得焦点,如果为null的时候，背景也要变成蓝色
                gift_money_edt.setHint("");
                gift_money_edt.setTextColor(Color.parseColor("#0077ff"));
                gift_money_edt.setBackground(mContext.getDrawable(R.drawable.x3_shape_idea_select_item));
            } else {
                // 失去焦点
                if ("".equals(gift_money_edt.getText().toString())) {
                    gift_btn_sure.setEnabled(false);
                    gift_money_edt.setHint(customMoney);
                    gift_btn_sure.setBackgroundColor(Color.parseColor("#D5D5D5"));
                }
            }
        });
        gift_money_edt.addTextChangedListener(phonrWatcher);
        selectGiftBalanceAdapter.setiOnClickSelect((selectMoneys, select) -> {
            selectMoney = Integer.parseInt(selectMoneys);
            if (!hasFocu) {
                //点击选中的现金之后，自定义初始化
                gift_money_edt.setHint(customMoney);
                gift_money_edt.setText("");
                gift_money_edt.setTextColor(Color.parseColor("#666666"));
                gift_money_edt.setBackground(mContext.getDrawable(R.drawable.x3_shape_idea_unselect_item));
                //按钮可点击
                if (select) {
                    gift_btn_sure.setBackgroundColor(Color.parseColor("#0077ff"));
                    gift_btn_sure.setEnabled(true);
                } else {
                    gift_btn_sure.setEnabled(false);
                    gift_btn_sure.setBackgroundColor(Color.parseColor("#D5D5D5"));
                }
            }
        });
    }

    //手机号码的监听
    TextWatcher phonrWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() > 0) {
                int sum = Integer.parseInt(gift_money_edt.getText().toString());
                if (sum > 0) {
                    selectMoney = Integer.parseInt(gift_money_edt.getText().toString());
                    selectGiftBalanceAdapter.changeSelect(GiftBalanceList);
                    gift_money_edt.setTextColor(Color.parseColor("#0077ff"));
                    gift_money_edt.setBackground(mContext.getDrawable(R.drawable.x3_shape_idea_select_item));
                    gift_btn_sure.setEnabled(true);
                    gift_btn_sure.setBackgroundColor(Color.parseColor("#0077ff"));
                }else {
                    gift_btn_sure.setEnabled(false);
                    gift_btn_sure.setBackgroundColor(Color.parseColor("#D5D5D5"));
                }
            } else {
                gift_money_edt.setTextColor(Color.parseColor("#666666"));
                gift_money_edt.setBackground(mContext.getDrawable(R.drawable.x3_shape_idea_unselect_item));
            }
        }
    };

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.gift_btn_sure) {//如果自定义输入的金额或者选中的金额超过限定的提示
            Log.e("TAG", "选中余额：" + selectMoney);
            if (selectMoney > presentLimitDailyRemainder) {
                X3ToastUtils.showMessage(mContext.getString(R.string.x3_gift_money_tosat_tv));
                return;
            }
            this.dismiss();
            // 确认赠送
            bottomGiftBalanceTWDialog = new X3BottomGiftBalanceTWDialog(mContext);
            bottomGiftBalanceTWDialog.show();
            bottomGiftBalanceTWDialog.setDate(giftBalanceBean, selectMoney, carOwnerNameUserId);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("赠送余额弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("赠送余额弹窗");
    }
}
